﻿using UnityEngine;
using System.Collections;
using FairyGUI;
using Baiyy;
using Baiyy.Proceduar;
using Baiyy.Resource;
using XLua;

[Hotfix]
/// <summary>
/// 更新流程。
/// </summary>
public class UpdateProceduar : BaseProceduar {

    #region vars
    UpdateWindow _win;
    #endregion

    #region methods
    public UpdateProceduar() {
		name = "UpdateProceduar";
	}

	void StartUpdate() {
		ResourceUpdator.inst.StartUpdate ("http://123.57.213.175/update/chess");
	}
	#endregion

	#region delegates
	void OnSetRemoteVersion() {
		string rVer = ResourceUpdator.inst.remoteVersion;
		if (rVer != null) {
			_win.version += "\n最新版本：" + rVer;
		} else {
			_win.version += "\n最新版本： 获取失败";
		}
	}
	void OnUpdateStageSwitch(ResourceUpdator.UpdateStage stage) {
		switch (stage) {
		case ResourceUpdator.UpdateStage.LoadingRemoteVersion:
			_win.info = "正在检查远程版本...";
			break;
		case ResourceUpdator.UpdateStage.CheckingUpdates:
			_win.info = "正在检查更新...";
			break;
		case ResourceUpdator.UpdateStage.Updating:
			_win.info = "正在更新...";
			_win.progress = 0;

			Console.Debug ("需要更新的文件：" + ResourceUpdator.inst.totalItems);
			break;
		case ResourceUpdator.UpdateStage.UpdateFinished:
			if (ResourceUpdator.inst.isUpdateOk) {
				_win.info = "更新完毕";
				_win.progress = 100;

				// 切换到加载流程
				Baiyy.Proceduar.BaseProceduar.Switch (new LoadingProceduar ());
			} else {
				// TODO 更新失败
				_win.info = "更新失败";
			}
			break;
		}
	}
	void OnBeginUpdateItem(ResourceUpdator.UpdateItem item) {
		_win.info = "正在更新 " + item.name;
		if (item.retryCount != 0) {
			_win.info += ", " + item.retryCount + "/" + ResourceUpdator.retryCount;
		}
	}
	void OnFailedUpdateItem(ResourceUpdator.UpdateItem item, string error) {
	}
	void OnFinishedUpdateItem(ResourceUpdator.UpdateItem item, int updatedItems) {
		_win.info = item.name + " 更新完毕";
		_win.progress = updatedItems * 100.0 / ResourceUpdator.inst.totalItems;
	}
	#endregion

	#region overrides
	/// <summary>
	/// 进入流程。
	/// </summary>
	public override void Enter() {
		ResourceUpdator.inst.onSetRemoteVersion += OnSetRemoteVersion;
		ResourceUpdator.inst.onStageSwitch += OnUpdateStageSwitch;
		ResourceUpdator.inst.onBeginUpdateItem += OnBeginUpdateItem;
		ResourceUpdator.inst.onFailedUpdateItem += OnFailedUpdateItem;
		ResourceUpdator.inst.onFinishedUpdateItem += OnFinishedUpdateItem;

		ResourceManager.inst.Load ("/ui/update.ab", false, (item, error) => {
			if (item != null) {
				UIPackage.AddPackage (item.bundle);
                _win = new UpdateWindow();
                _win.info = "";
                _win.progress = 100;
				_win.version = "本地版本：" + ResourceUpdator.inst.localVersion;

                UISystem.inst.Show (_win);

				StartUpdate();
			} else {
				Console.Error("[UpdateProceduar] 加载资源失败, " + error);
			}
		});
	}
	/// <summary>
	/// 更新流程。
	/// </summary>
	public override void Update() {
	}
	/// <summary>
	/// 离开流程。
	/// </summary>
	public override void Leave() {
		ResourceUpdator.inst.onSetRemoteVersion -= OnSetRemoteVersion;
		ResourceUpdator.inst.onStageSwitch -= OnUpdateStageSwitch;
		ResourceUpdator.inst.onBeginUpdateItem -= OnBeginUpdateItem;
		ResourceUpdator.inst.onFailedUpdateItem -= OnFailedUpdateItem;
		ResourceUpdator.inst.onFinishedUpdateItem -= OnFinishedUpdateItem;
	}
	#endregion
}
